//@see next js function is for role query add edit delete 15:05 2008-2-19

/**
* @todo：角色初始化,角色列表
* @date：15:05 2008-2-19
* @author：hyh
*/
function loadAclroleList(page,pageSize){
	if(typeof(page) == "undefined"){
		page = 1;
	}
	if(typeof(pageSize) == "undefined"){
		pageSize = 15;
	}
	DWRUtil.useLoadingMessage("正在读取数据...");
	var oJSRoleSearchBean = new JSRoleSearchBean();
	oJSRoleSearchBean.rolebh = DWRUtil.getValue("oJSRoleSearchBean.rolebh");
	oJSRoleSearchBean.rolename = DWRUtil.getValue("oJSRoleSearchBean.rolename");
	ConfigAction.listAclrole(oJSRoleSearchBean,page,pageSize,function(pageObject){
		  var data = pageObject.resultList;
		  var i = 1;
		  var url;
		  DWRUtil.removeAllRows("role_table");
		  var cellFuncs = [
		  function(data) {
			  return "<input type=\"checkbox\" name=\"rolechkbox\" value=\""+data.id+"\" class=\"checkbox\">";
		  },
		  function(data) {
			  if(data.rolename == "null" || data.rolename == null){
				  return "";
			  }else{
				  return data.rolebh + "&nbsp;&nbsp;" + data.rolename;
			  }	  
		  },
		  function(data) {
			return "<a href=\"#\" onclick=\"getUserRelaRoleHtml('"+data.id+"','"+data.rolename+"');return false;\"><img src=\"../themes/default/images/12.bmp\" width=\"15\" height=\"16\" border=\"0\" alt=\"角色用户关联\"></a>&nbsp;&nbsp;<a href=\"#\" onclick=\"roleOpInit('"+data.id+"');return false;\"><img src=\"../themes/default/images/edit2.gif\" border=\"0\" width=\"15\" height=\"15\" valign=\"bottom\" alt=\"编辑\"></a>";
		  }
		  ];
		  DWRUtil.addRows("role_table",data,cellFuncs,{
		  rowCreator:function(options) {
		    return document.createElement("tr");
		  },
		  cellCreator:function(options) {
		    var td = document.createElement("td");
		    var cellNum = options.cellNum;
		    td.align = "center";
			//td.height = "28";
		    return td;
		  },
		  escapeHtml:false
		  });
		  /**分页-----------------------------------------------------*/
		  var func_ = "loadAclroleList(page,15)";
		  var htmlStr = showPage(pageObject,func_);//对象和方法
		  DWRUtil.setValue("role_page_div",htmlStr,{escapeHtml:false});
		  addTableRolloverEffect('table_role_query','tableRollOverEffect1','tableRowClickEffect1');
		  initSortTable('table_role_query',Array('','S',''));
	});
}

/**
* @todo：角色添加删除详情初始化
* @date：14:16 2008-2-20
* @author：hyh
*/
function roleOpInit(id){
	setRoledetail($("div_roleop_hidden").innerHTML);
	if(id != null && id != ""){//for edit detail
		setRoleDetailHtml();//detail
		getRoleDetail(id);
	}
	menuResoureTreeLoad(id);
}

/**
* @todo：load菜单资源树
* @date：14:55 2008-2-20
* @author：hyh
*/
function menuResoureTreeLoad(id){
	DWRUtil.useLoadingMessage("正在读取资源树...");
	ConfigAction.getAclmenuresourceListToCheckboxHtml("0",function(data){
		$("menuresource_tree").innerHTML = data;
		treeObj = new JSDragDropTree();
		treeObj.setImageFolder('../components/drag-drop-folder-tree/images/');
		treeObj.setTreeId('dhtml_tree');
		treeObj.setMaximumDepth(10);
		treeObj.setMessageMaximumDepthReached('超过最多级别'); 
		treeObj.initTree();
		treeObj.expandAll();
		if(id != null && id !=""){//判断资源树的checkbox
			ConfigAction.listAclrelarolemenuByRoleId(id,function(data){
				var len = data.length;
				for(var i=0;i<len;i++){
					var chkArray = document.getElementsByName("chkbox");
					var chkLen = chkArray.length;
					if(len == chkLen){//全选
						$("chkall").checked = true;
						$("chkall").disabled = true;
						for(var j=0;j<chkLen;j++ ){
							chkArray[j].checked = true;
							chkArray[j].disabled = true;
       			 		}//for
						break;
					}else{
						for(var j=0;j<chkLen;j++ ){
							chkArray[j].disabled = true;
							if(chkArray[j].value == data[i].menuid){
								chkArray[j].checked = true;
								break;
							}
       			 		}//for
					}
				}//for
			});
		}
	});
}

/**
* @todo：读取角色详情
* @date：14:16 2008-2-20
* @author：hyh
*/
function getRoleDetail(/*String*/id){
	DWRUtil.useLoadingMessage("正在读取数据...");
	ConfigAction.getAclroleById(id,function(oJSRoleBean){
		setFormValue(oJSRoleBean);
	});
}

/**
* @todo：点击一个节点自动选择父节点，及选择所有子节点
* @date：10:49 2008-2-20
* @author：hyh
*/
function checkUpDown(obj){
	//节点名称:tagName
	if(obj.checked){
		//up
		var thisObj = obj.parentNode.parentNode;//li input->a->li
		var pObj = thisObj.parentNode.parentNode;//li
		while(pObj.id != 0){
			for(i=0;i<pObj.childNodes.length;i++){
				if(pObj.childNodes[i].tagName == "A"){
					pObj.childNodes[i].getElementsByTagName("input")[0].checked = true;
				}
			}
			pObj = pObj.parentNode.parentNode;
		}
		//down
		var input = thisObj.getElementsByTagName("input");
		for(var i = 0;i<input.length;i++){
			input[i].checked = true;
		}
	}else{//down
		var input = obj.parentNode.parentNode.getElementsByTagName("input");
		for(var i = 0;i<input.length;i++){
			input[i].checked = false;
		}
	}
}

/**
* @todo：角色保存（添加或编辑）
* @date：14:20 2008-2-20
* @author：hyh
*/
function saveRole(){
	DWRUtil.useLoadingMessage("正在保存角色...");
	var oJSRoleBean = new JSRoleBean();
	getFormValue(oJSRoleBean);
	var resourceIds = getCheckValuesByName("chkbox");
	if(oJSRoleBean.rolename == ""){
		alert("角色名称不能为空！");
		$("rolename").focus();
		return false;
	}
	if(oJSRoleBean.rolebh == ""){
		alert("角色编号不能为空！");
		$("rolebh").focus();
		return false;
	}
	if(resourceIds == ""){
		alert("请选择资源树！");
		return false;
	}
	if(oJSRoleBean.id == ""){//新增
		ConfigAction.insertAclrole(oJSRoleBean,resourceIds,function(data){
			alert(data);
			setRoledetail("");
			loadAclroleList();
		});
	}else{//编辑
		ConfigAction.updateAclrole(oJSRoleBean,resourceIds,function(data){
			alert(data);
			setRoleDetailHtml();
			loadAclroleList();
		});
	}
	//showBeanValue(oJSRoleBean);
}

/**
* @todo：删除角色
* @date：14:55 2008-2-21
* @author：hyh
*/
function deleteRole(){
	var roleIds = getCheckValuesByName("rolechkbox");
	if(roleIds == ""){
		alert("请至少要选择一条删除的角色记录！");
		return false;
	}
	if(confirm("你确定删除角色吗？")){
		DWRUtil.useLoadingMessage("正在删除角色数据...");
		ConfigAction.deleteAclrole(roleIds,function(data){
			alert(data);
			setRoledetail("");
			loadAclroleList();
		});
	}
}

//@see next js function is for user list relation role 14:55 2008-2-21

/**
* @todo：角色用户页面
* @date：14:43 2008-2-21
* @author：hyh
*/
function getUserRelaRoleHtml(id,rolename){
	setRoledetail($("div_userrelarole_hidden").innerHTML);
	DWRUtil.setValue("oJSUserRoleFormBean.roleid",id);//hidden
	DWRUtil.setValue("oJSUserRoleFormBean.rolename",rolename);//hidden
	loadAcluserByAclrole();
}

/**
* @todo：读取角色关联的用户
* @date：14:43 2008-2-21
* @author：hyh
*/
function loadAcluserByAclrole(page,pageSize){
	if(typeof(page) == "undefined"){
		page = 1;
	}
	if(typeof(pageSize) == "undefined"){
		pageSize = 15;
	}
	DWRUtil.useLoadingMessage("正在读取数据...");
	var oJSUserRoleFormBean = new JSUserRoleFormBean();
	oJSUserRoleFormBean.roleid = DWRUtil.getValue("oJSUserRoleFormBean.roleid");
	oJSUserRoleFormBean.account = DWRUtil.getValue("oJSUserRoleFormBean.account");
	oJSUserRoleFormBean.realname = DWRUtil.getValue("oJSUserRoleFormBean.realname");
//	showBeanValue(oJSUserRoleFormBean);
	ConfigAction.listAcluserByRoleId(oJSUserRoleFormBean,page,pageSize,function(pageObject){
		  var data = pageObject.resultList;
		  var i = 1;
		  DWRUtil.removeAllRows("roleuser_table");
		  var cellFuncs = [
		  function(data) {
			  return "<input type=\"checkbox\" name=\"roleuserchkbox\" value=\""+data.userid+"\" class=\"checkbox\">";
		  },
		  function(data) {//帐号
			  if(data.account == "null" || data.account == null){
				  return "";
			  }else{
				  return data.account;
			  }
		  },
		  function(data) {//用户名
			  if(data.realname == "null" || data.realname == null){
				  return "";
			  }else{
				  return data.realname;
			  }
		  },
		  function(data) {//用户状态
			  if(data.userstatus == "null" || data.userstatus == null){
				  return "";
			  }else if(data.userstatus == "1"){
			  	  return "激活";
			  }else if(data.userstatus == "2"){
			  	  return "未激活";
			  }else{
			  	  return "已删除";
			  }
		  },
		  function(data) {//角色名
			  if(data.rolename == "null" || data.rolename == null){
				  return "";
			  }else{
				  return data.rolename;
			  }
		  },
		  function(data) {//操作
			return "<a href=\"#\" onclick=\"openAcluserop('"+data.userid+"');return false;\"><img src=\"../themes/default/images/edit2.gif\" border=\"0\" width=\"15\" height=\"15\" valign=\"bottom\" alt=\"编辑\">";
		  }
		  ];
		  DWRUtil.addRows("roleuser_table",data,cellFuncs,{
		  rowCreator:function(options) {
		    return document.createElement("tr");
		  },
		  cellCreator:function(options) {
		    var td = document.createElement("td");
		    var cellNum = options.cellNum;
		    td.align = "center";
			//td.height = "28";
		    return td;
		  },
		  escapeHtml:false
		  });
		  /**分页-----------------------------------------------------*/
		  var func_ = "loadAcluserByAclrole(page,15)";
		  var htmlStr = showPage(pageObject,func_);//对象和方法
		  DWRUtil.setValue("roleuser_page_div",htmlStr,{escapeHtml:false});
		  addTableRolloverEffect('table_roleuser_query','tableRollOverEffect1','tableRowClickEffect1');
		  initSortTable('table_roleuser_query',Array('','S','S','S','S',''));
	});
}

/**
* @todo：删除角色和用户的关联
* @date：15:49 2008-2-21
* @author：hyh
*/
function deleteRoleRelaUser(){
	var roleId = DWRUtil.getValue("oJSUserRoleFormBean.roleid");
	var userIds = getCheckValuesByName("roleuserchkbox");
	if(userIds == ""){
		alert("请至少要选择一条删除的用户记录！");
		return false;
	}
	if(confirm("你确定删除该角色关联的用户吗？")){
		DWRUtil.useLoadingMessage("正在删除角色用户关联数据...");
		ConfigAction.deleteAclrelaroleuserByRoleId2UserId(roleId,userIds,function(data){
			alert(data);
			loadAcluserByAclrole();
		});
	}
}

//@see next js function is for role common function 15:09 2008-2-19

/**set Role detail div 15:09 2008-2-19*/
function setRoledetail(/*String*/str){
	$("roledetail").innerHTML = str;
}

//open add or edit or detail page
function openAcluserop(id){
	var url = "acluserop.html"
	if(id){
		url += "?id=" + id + "&pagetype=role";
	}
	openWin_h(url,"800","500");
}

/**user query 16:24 2008-2-26*/
function openUserQueryHtml(){
	var url = "acluserquery.html?type=usernoralerole&roleid=" + DWRUtil.getValue("oJSUserRoleFormBean.roleid");
	openWin_h(url,'800','500');
}

/**Role Object 9:01 2008-2-20*/
function JSRoleBean(){
	this.id = "";
	this.rolename = "";
	this.rolebh = "";
	this.remark = "";
}
/**Role search Object 9:26 2008-2-21*/
function JSRoleSearchBean(){
	this.rolename = "";
	this.rolebh = "";
}

/**userrole search Object 10:57 2008-2-21*/
function JSUserRoleFormBean(){
	this.roleid = "";
	this.account = "";
	this.realname = "";
}

/**展示详情页面 9:55 2008-2-25*/
function setRoleDetailHtml(){
	$("roleop_title").innerHTML = "<img src=\"../themes/default/images/Contacts.gif\" width=\"16\" height=\"16\">角色信息";
	$("op_button").innerHTML = $("div_hidden_detail").innerHTML;
	setInputReadOnly("rolename");
	setInputReadOnly("rolebh");
	setInputReadOnly("remark");
}

/**展示编辑页面 9:55 2008-2-25*/
function setRoleEditHtml(){
	$("roleop_title").innerHTML = "<img src=\"../themes/default/images/Contacts.gif\" width=\"16\" height=\"16\">编辑角色";
	$("op_button").innerHTML = $("div_hidden_edit").innerHTML;
	setInputReadOnly("rolename","false");
	setInputReadOnly("rolebh","false");
	setInputReadOnly("remark","false");
	
	//菜单资源树checkbox可编辑
	$("chkall").disabled = false;
	var oArray = document.getElementsByName("chkbox");
	if(oArray.length){
		for(var i = 0 ; i < oArray.length ; i++ ){
			oArray[i].disabled = false;
		}
	}	
}